#include "hiconitemview.h"
#include "devcommon.h"

#include <QPainter>
#include <QPainterPath>
#include <QFontMetrics>
#include <QDebug>
#include <QColor>

HIconListViewItem::HIconListViewItem(QObject *parent):
    QObject (parent)
{
    mMargins = QMargins(6, 6, 6, 6);
    mSpacing = 9;
}

HIconListViewItem::HIconListViewItem(const HIconListViewItem &other, QObject *parent):
    QObject (parent)
{
    mText = other.mText;
    mIcon = other.mIcon;
    mMargins = other.mMargins;
    mSpacing = other.mSpacing;
}

HIconListViewItem& HIconListViewItem::operator=(const HIconListViewItem &other)
{
    mText = other.mText;
    mIcon = other.mIcon;
    mMargins = other.mMargins;
    mSpacing = other.mSpacing;
    return *this;
}

QString HIconListViewItem::text() const
{
    return mText;
}

void HIconListViewItem::setText(const QString &text)
{
    mText = text;
}

QIcon HIconListViewItem::icon() const
{
    return mIcon;
}

void HIconListViewItem::setIcon(const QIcon &icon)
{
    mIcon = icon;
}

QMargins HIconListViewItem::margins() const
{
    return mMargins;
}

void HIconListViewItem::setMargins(const QMargins &margins)
{
    mMargins = margins;
}

int HIconListViewItem::spacing() const
{
    return mSpacing;
}

void HIconListViewItem::setSpacing(int spacing)
{
    mSpacing = spacing;
}


HIconItemView::HIconItemView(HIconListViewItem *item, QWidget *parent) :
    QWidget(parent),
    mItem (*item)
{
    mBackground = Qt::white;
}

HIconItemView::HIconItemView(const HIconListViewItem &item, QWidget *parent) :
    QWidget(parent),
    mItem(item)
{
    mBackground = Qt::white;
}

HIconItemView::ViewMode HIconItemView::viewMode() const
{
    return mViewMode;
}

void HIconItemView::setViewMode(const ViewMode &viewMode)
{
    mViewMode = viewMode;
    update();
}

QSize HIconItemView::size() const
{
    return mSize;
}

void HIconItemView::setSize(const QSize &size)
{
    mSize = size;
    setFixedSize(size);
    update();
}

QSize HIconItemView::iconSize() const
{
    return mIconSize;
}

void HIconItemView::setIconSize(const QSize &iconSize)
{
    mIconSize = iconSize;
    update();
}



void HIconItemView::mousePressEvent(QMouseEvent *event)
{
    mBackground = QColor(0, 120, 210);
    update();
    QWidget::mousePressEvent(event);
}

void HIconItemView::mouseReleaseEvent(QMouseEvent *event)
{
    mBackground = QColor(250, 250, 250);
    update();
    emit itemClicked(mIndex);
    QWidget::mouseReleaseEvent(event);
}

void HIconItemView::mouseDoubleClickEvent(QMouseEvent *event)
{
    QWidget::mouseDoubleClickEvent(event);
}

void HIconItemView::keyPressEvent(QKeyEvent *event)
{
    QWidget::keyPressEvent(event);
}

void HIconItemView::keyReleaseEvent(QKeyEvent *event)
{
    QWidget::keyReleaseEvent(event);
}

void HIconItemView::focusInEvent(QFocusEvent *event)
{
    mBackground = QColor(0, 160, 230);
    update();
    QWidget::focusInEvent(event);
}

void HIconItemView::focusOutEvent(QFocusEvent *event)
{
    mBackground = QColor(250, 250, 250);
    update();
    QWidget::focusOutEvent(event);
}

void HIconItemView::enterEvent(QEvent *event)
{
    mBackground = QColor(0, 160, 230);
    update();
    QWidget::enterEvent(event);
}

void HIconItemView::leaveEvent(QEvent *event)
{
    mBackground = QColor(250, 250, 250);
    update();
    QWidget::leaveEvent(event);
}

void HIconItemView::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    QRect rect = this->rect();
    if (mItem.margins().top() + mItem.margins().bottom() >= rect.height() \
            || mItem.margins().left() + mItem.margins().right() >= rect.width()) {
        QWidget::paintEvent(event);
        return;
    }
    painter.save();
    painter.fillRect(rect, QBrush(mBackground));
    rect.setWidth(this->rect().width() - (mItem.margins().left() + mItem.margins().right()));
    rect.setHeight(this->rect().height() - (mItem.margins().top() + mItem.margins().bottom()));
    rect.moveCenter(this->rect().center());
    if (mViewMode == IconMode && !(mItem.icon().isNull())) {
        QRect diconrect = QRect(0, 0, mIconSize.width(), mIconSize.height());
        diconrect.moveCenter(rect.center());
        QRect textRect = Devcommon::calcStringSize(mItem.text(), this->font());
        diconrect.setY(diconrect.y() - mItem.spacing()/2 - textRect.height()/2);
        painter.drawPixmap(diconrect, mItem.icon().pixmap(mIconSize));
        QPoint textp = QPoint(0, diconrect.y() + diconrect.height()+mItem.spacing());
        if (diconrect.width() >= textRect.width()) {
            textp.setX(diconrect.x()+(diconrect.width()-textRect.width())/2);
        } else {
            textp.setX(diconrect.x());
        }
        painter.drawText(textp, mItem.text());
    }
    painter.restore();
    QWidget::paintEvent(event);
}

int HIconItemView::index() const
{
    return mIndex;
}

void HIconItemView::setIndex(int index)
{
    mIndex = index;
}





